package com.example.ticket.controller;

import com.example.ticket.bean.ResponseBean;
import com.example.ticket.entity.TicketFile;
import com.example.ticket.service.FileService;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class UploadController {
    @Autowired
    FileService fileService;


/*
    public final static String UPLOAD_PATH_PREFIX = "static/file/";

*/

    //文件上传
/*    @PostMapping("/UploadFile/{id}")
    @ResponseBody
    public Map ajaxUploadFile(@PathVariable Integer id,MultipartFile[] files){
        Map<String,Object> map=new HashMap<>();
        for(MultipartFile file:files){
            String fileName=file.getOriginalFilename();
            fileName= UUID.randomUUID()+"_"+fileName;

            //项目里的file路径
            *//*String dirPath="E:/ticketfile/ ";*//*
            String dirPath= ClassUtils.getDefaultClassLoader().getResource("static/file/").getPath();
*//*            String dirPath = new String("src/main/resources/" + UPLOAD_PATH_PREFIX);*//*
            File filePath=new File(dirPath);
*//*            if(!filePath.exists()){//如果没有这个路径，就自行创建
                filePath.mkdirs();
            }*//*
            try{
                //写入磁盘
                file.transferTo(new File(dirPath+fileName));


                String fileurl=fileName;
                TicketFile ticketFile=new TicketFile();
                ticketFile.setTicketId(id);
                ticketFile.setFileurl(fileurl);

                fileService.insertFile(ticketFile);

                //上传成功后返回提示信息
                map.put("msg","上传成功!");
            }catch (Exception e){
                e.printStackTrace();
                map.put("msg","上传失败!");
            }
        }
        return map;
    }*/


    @PostMapping("/UploadFile/{id}")
    @ResponseBody
    public Map<String, Object> ajaxUploadFile(@PathVariable Integer id,MultipartFile[] files){
        String fileName;
        Map<String, Object> map=new HashMap<>();
        for(MultipartFile file:files){
            fileName = file.getOriginalFilename();
            fileName= UUID.randomUUID()+"_"+fileName;
            String path = System.getProperty("user.dir");
            String dirPath = path+"/src/main/resources/static/file/";
            File filePath = new File(dirPath);
            if(!filePath.exists()){
                filePath.mkdirs();
            }
            try{
                file.transferTo((new File(dirPath+fileName)));
                String fileurl=dirPath+fileName;
                TicketFile ticketFile=new TicketFile();
                ticketFile.setTicketId(id);
                ticketFile.setFileurl(fileurl);
                fileService.insertFile(ticketFile);
                map.put("msg","上传成功！");
            }catch (Exception e){
                e.printStackTrace();
                map.put("msg","上传失败！");
            }
        }
        return map;
    }

/*    //文件下载
    @GetMapping("download")
    public ResponseEntity<byte[]> fileDownload(HttpServletRequest request,String filename)throws Exception{

        //下载的文件的根路径
        String dirPath="F:\\大三下\\Jave EE\\实训\\票据管理\\ticketManageSystem\\src\\main\\resources\\static\\file\\";
        //创建文件对象
        File file=new File(dirPath+File.separator+filename);
        //设置响应头
        HttpHeaders headers=new HttpHeaders();
        //通知浏览器以下载的方式打开
        filename=getFilename(request,filename);
        headers.setContentDispositionFormData("attachment",filename);
        //定义以流形式下载返回文件数据
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try{
            return new ResponseEntity<>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
        }
    }*/

}
